<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        
        
        <link rel="canonical" href="https://mockserver.hissummer.com/documents/catalog/">
        <link rel="shortcut icon" href="../../img/favicon.ico">
        <title>文档 - Hissummer-mockserver</title>
        <link href="../../css/bootstrap-custom.min.css" rel="stylesheet">
        <link href="../../css/font-awesome.min.css" rel="stylesheet">
        <link href="../../css/base.css" rel="stylesheet">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github.min.css">
        <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
        <!--[if lt IE 9]>
            <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
            <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->

        <script src="../../js/jquery-1.10.2.min.js" defer></script>
        <script src="../../js/bootstrap-3.0.3.min.js" defer></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
        <script>hljs.initHighlightingOnLoad();</script> 
    </head>

    <body>

        <div class="navbar navbar-default navbar-fixed-top" role="navigation">
            <div class="container">

                <!-- Collapsed navigation -->
                <div class="navbar-header">
                    <!-- Expander button -->
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="../..">Hissummer-mockserver</a>
                </div>

                <!-- Expanded navigation -->
                <div class="navbar-collapse collapse">
                        <!-- Main navigation -->
                        <ul class="nav navbar-nav">
                            <li >
                                <a href="../..">主页</a>
                            </li>
                            <li >
                                <a href="../../quickstart/">快速开始</a>
                            </li>
                            <li class="active">
                                <a href="./">文档</a>
                            </li>
                            <li >
                                <a href="https://github.com/hissummer-mockserver/mockServer/issues">后端问题提交</a>
                            </li>
                            <li >
                                <a href="https://github.com/hissummer-mockserver/mockserverAdminUI/issues">前端问题提交</a>
                            </li>
                        </ul>

                    <ul class="nav navbar-nav navbar-right">
                        <li>
                            <a href="#" data-toggle="modal" data-target="#mkdocs_search_modal">
                                <i class="fa fa-search"></i> Search
                            </a>
                        </li>
                            <li >
                                <a rel="next" href="../../quickstart/">
                                    <i class="fa fa-arrow-left"></i> Previous
                                </a>
                            </li>
                            <li class="disabled">
                                <a rel="prev" >
                                    Next <i class="fa fa-arrow-right"></i>
                                </a>
                            </li>
                    </ul>
                </div>
            </div>
        </div>

        <div class="container">
                <div class="col-md-3"><div class="bs-sidebar hidden-print affix well" role="complementary">
    <ul class="nav bs-sidenav">
        <li class="main active"><a href="#_1">一 工作原理</a></li>
        <li class="main "><a href="#http-mockdd">二 Http Mock规则dd</a></li>
            <li><a href="#_2">规则属性</a></li>
            <li><a href="#_3">操作</a></li>
            <li><a href="#_4">匹配规则</a></li>
        <li class="main "><a href="#eureka-mock">三 Eureka Mock规则</a></li>
            <li><a href="#_5">规则属性</a></li>
            <li><a href="#_6">操作</a></li>
    </ul>
</div></div>
                <div class="col-md-9" role="main">

<h2 id="_1">一 工作原理</h2>
<ol>
<li>
<p>根据http请求路径进行匹配,如果没有找到则返回未找到mock规则。
例子： 如果访问 http://localhost:8080/a/b</p>
<ol>
<li>第一步查找 localhost的主机名的 /a/b 规则是否存在，如果找到则返回该mock规则定义的mock报文和响应Header。</li>
<li>如果第一步未找到则查找 localhost的主机名的 /a 规则是否存在，如果找到则返回该mock规则定义的mock报文和响应Header。 </li>
<li>如果第二步未找到规则，则查找 *主机名的/a/b 规则是否存在，如果找到则返回该mock规则定义的mock报文和响应Header。</li>
<li>如果第三步未找到规则，则*的主机名的 /a 规则是否存在，如果找到则返回该mock规则定义的mock报文和响应Header。</li>
<li>如果以上步骤均未找到，则返回未找到mock规则。</li>
</ol>
</li>
<li>
<p>返回mock报文的处理</p>
<ol>
<li>第一步首先处理${__内建函数}变量替换。例如${__NowDate()} 替换成 1539313200000</li>
<li>
<p>第二步请求header，请求body中的取值替换 。 例如${requestBody.a} 将本次请求的body中的a值获取到并替换。</p>
<pre><code>例如： 请求的header content-type: application/json,  body 为  {'a':'value'} , 那么 mock response的报文中 ${requestBody.a}  将会替换成  value值
</code></pre>
</li>
<li>
<p>第三步，如果是groovy脚本，则执行groovy脚本。 并将groovy脚本中的response变量的值作为要返回的response值。</p>
</li>
</ol>
</li>
</ol>
<h2 id="http-mockdd">二 Http Mock规则dd</h2>
<p>Http Mock Rule，定义一个http接口的mock报文。</p>
<h3 id="_2">规则属性</h3>
<ul>
<li>id</li>
<li>hostName</li>
<li>uri</li>
<li>工作模式</li>
<li>响应Header</li>
<li>
<p>响应mock报文</p>
<p>Mock报文如2种方式：</p>
<ol>
<li>
<p>静态response报文呢</p>
<blockquote>
<p>即创建mock规则的响应报文内容不做任何处理直接返回。</p>
</blockquote>
</li>
<li>
<p>动态response报文</p>
<ol>
<li>
<p>内建函数</p>
<blockquote>
<p>内建函数的命名规则同Jmeter，不支持嵌套使用。 但一个responseBody总可以使用多个内建函数。 ${__NowDate(arg1,arg2)} NowDate为函数名称。</p>
</blockquote>
<ol>
<li>
<p>NowDate("yyyy-MM-dd HH:mm:ss")  日期为可选参数，若不填写即时当前时间秒。 若填写返回指定的时间的java时间秒。</p>
<blockquote>
<p>示例：
NowDate("2018-10-12 11:00:00") -&gt;  1539313200000
NowDate()  -&gt;  1539313200000 (当前时间的java秒)</p>
</blockquote>
</li>
<li>
<p>RandomString(stringLength,stringCharactor)</p>
<blockquote>
<p>stringLength 返回的随机字符串长度
stringCharactor 返回的字符串的字符内容</p>
<p>示例：
RandomString("5","abcdefg")  -&gt;  adefg
RandomString("5") -&gt; XZ6MKZOOF77MFABQ3V</p>
</blockquote>
</li>
</ol>
</li>
<li>
<p>通过请求header或者请求body中获取数据</p>
<blockquote>
<p>通过${responseBody.a}</p>
<p>通过${responseHeader.a}</p>
</blockquote>
</li>
<li>
<p>groovy脚本</p>
</li>
</ol>
</li>
</ol>
</li>
</ul>
<pre><code>            //groovy
            a='myresponse'
            response=a
</code></pre>

<pre><code>    groovy中设置response变量的值就是要返回的值，response的值可以用groovy脚本生成。
</code></pre>
<h3 id="_3">操作</h3>
<ol>
<li>
<p>添加</p>
</li>
<li>
<p>删除</p>
</li>
<li>
<p>修改</p>
</li>
<li>
<p>复制添加</p>
</li>
</ol>
<h3 id="_4">匹配规则</h3>
<ol>
<li>
<p>匹配流程</p>
<blockquote>
<p>http mock匹配规则说明
1. 同样的hostName和uri 只能添加一条  <br />
1. hostName 为<em> 时, 即表示可以匹配所有的hostName.  <br />
1. hostName(不为</em>) 和 uri如果 没有匹配到,会尝试匹配hostName为*的规则    </p>
<p><strong>示例如下:</strong>
若添加有2条规则
* 第一条规则: hostName:*  uri:/hello  mockResponse: mock1
* 第二条规则: hostName:testHostName  uri:/hello  mockResponse: mock2</p>
<blockquote>
<ul>
<li>当访问 http://127.0.0.1/hello 时(通过ip地址访问mock server), 则会匹配第一条规则， 返回报文： <em>mock1</em>.</li>
<li>如果我访问者 http://testHostName/hello 时,会匹配到第二条规则，返回报文：<em>mock2</em>. (testHostName 需要添加hosts或者dns添加record)</li>
<li>再如果我访问 http://testanotherHostName/hello 时, 则会尝试先匹配 hostName=testanotherHostName, uri=/hello  找不到则会寻找</li>
<li>hostName=testanotherHostName , uri=/ , 如果再找不到则开始寻找 hostName=<em> , uri=/hello，返回报文: </em>mock1*。</li>
</ul>
</blockquote>
</blockquote>
</li>
</ol>
<h2 id="eureka-mock">三 Eureka Mock规则</h2>
<p>Eureka Mock Rule， 定义如何注册一个eureka服务实例。</p>
<h3 id="_5">规则属性</h3>
<ul>
<li>id</li>
<li>eurekaServer</li>
<li>eureka ersion</li>
<li>hostName</li>
<li>port</li>
<li>serviceName</li>
<li>status (disable/enable)</li>
</ul>
<h3 id="_6">操作</h3>
<ol>
<li>
<p>添加</p>
</li>
<li>
<p>删除</p>
</li>
<li>
<p>修改</p>
</li>
<li>
<p>复制添加</p>
</li>
</ol></div>
        </div>

        <footer class="col-md-12">
            <hr>
            <p>Documentation built with <a href="https://www.mkdocs.org/">MkDocs</a>.</p>
        </footer>
        <script>
            var base_url = "../..",
                shortcuts = {"search": 83, "next": 78, "help": 191, "previous": 80};
        </script>
        <script src="../../js/base.js" defer></script>
        <script src="../../search/main.js" defer></script>

        <div class="modal" id="mkdocs_search_modal" tabindex="-1" role="dialog" aria-labelledby="Search Modal" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title" id="exampleModalLabel">Search</h4>
            </div>
            <div class="modal-body">
                <p>
                    From here you can search these documents. Enter
                    your search terms below.
                </p>
                <form role="form">
                    <div class="form-group">
                        <input type="text" class="form-control" placeholder="Search..." id="mkdocs-search-query" title="Type search term here">
                    </div>
                </form>
                <div id="mkdocs-search-results"></div>
            </div>
            <div class="modal-footer">
            </div>
        </div>
    </div>
</div><div class="modal" id="mkdocs_keyboard_modal" tabindex="-1" role="dialog" aria-labelledby="Keyboard Shortcuts Modal" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title" id="exampleModalLabel">Keyboard Shortcuts</h4>
            </div>
            <div class="modal-body">
              <table class="table">
                <thead>
                  <tr>
                    <th style="width: 20%;">Keys</th>
                    <th>Action</th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <td class="help shortcut"><kbd>?</kbd></td>
                    <td>Open this help</td>
                  </tr>
                  <tr>
                    <td class="next shortcut"><kbd>n</kbd></td>
                    <td>Next page</td>
                  </tr>
                  <tr>
                    <td class="prev shortcut"><kbd>p</kbd></td>
                    <td>Previous page</td>
                  </tr>
                  <tr>
                    <td class="search shortcut"><kbd>s</kbd></td>
                    <td>Search</td>
                  </tr>
                </tbody>
              </table>
            </div>
            <div class="modal-footer">
            </div>
        </div>
    </div>
</div>

    </body>
</html>
